package com.gz.thread.juc.collection;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

public class SetDemo {
    public static void main(String[] args) {
      //  notSafeSet();
      // jucSet();
        collSet();
    }


    public static void notSafeSet(){
        Set<String> objects = new HashSet<>();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                objects.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(objects);
            },String.valueOf(i)).start();
        }
    }

    public static void jucSet(){
        Set<String> objects = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                objects.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(objects);
            },String.valueOf(i)).start();
        }
    }

    public static void collSet(){
        Set<String> objects = Collections.synchronizedSet(new HashSet<>());
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                objects.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(objects);
            },String.valueOf(i)).start();
        }
    }
}
